package com.wan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wan.entity.OfficeBuild;
import com.wan.entity.OfficeRoomInfos;
import com.wan.service.system.OfficeBuildingRoomService;
import com.wan.service.system.OfficeBuildingService;
import entity.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import utils.TimeUtils;

import java.util.List;


/**
 * <p>
 * 办公楼教室表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-03-11
 */
@RequestMapping("/map/officeRoomInfos")
@Controller
public class OfficeRoomInfosController {

    private static final long PAGE_SIZE = 10;

    @Autowired
    private OfficeBuildingRoomService roomService;
    @Autowired
    private OfficeBuildingService buildingService;

    @RequestMapping("/save")
    @ResponseBody
    public ResultBean officeBuildRoomSave(OfficeRoomInfos officeRoomInfos) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        OfficeBuild officeBuild = buildingService.getById(officeRoomInfos.getOfficeId());
        officeRoomInfos.setOfficeBuildingName(officeBuild.getOfficeBuildingName());
        officeRoomInfos.setUsername(authentication.getName());
        officeRoomInfos.setCreateTime(TimeUtils.GetNowDate("yyyy-MM-dd HH:mm:ss"));
        officeRoomInfos.setUpdateTime(TimeUtils.GetNowDate("yyyy-MM-dd HH:mm:ss"));
        Boolean b = roomService.save(officeRoomInfos);
        if (b) {
            return new ResultBean(200,"办公楼房间添加成功");
        }else {
            return new ResultBean(500,"办公楼房间添加失败");
        }
    }


    @RequestMapping("/search/list")
    @ResponseBody
    public ResultBean searchList(Long currPage,String title){
        IPage<OfficeRoomInfos> page = new Page<>(currPage,PAGE_SIZE);
        QueryWrapper<OfficeRoomInfos> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("room_name",title);
        queryWrapper.orderByDesc("update_time");
        IPage<OfficeRoomInfos> officeRoomInfosIPage = roomService.page(page,queryWrapper);
        List<OfficeRoomInfos> records = officeRoomInfosIPage.getRecords();
        Long count = officeRoomInfosIPage.getTotal();
        long totalPage  = (count + PAGE_SIZE -1) / PAGE_SIZE;
        return new ResultBean(0,"成功",totalPage,records);
    }


    @RequestMapping("/edit")
    @ResponseBody
    public ResultBean officeBuildRoomUpdate(OfficeRoomInfos officeRoomInfos) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        OfficeBuild officeBuild = buildingService.getById(officeRoomInfos.getOfficeId());
        officeRoomInfos.setOfficeBuildingName(officeBuild.getOfficeBuildingName());
        officeRoomInfos.setUsername(authentication.getName());
        officeRoomInfos.setUpdateTime(TimeUtils.GetNowDate("yyyy-MM-dd HH:mm:ss"));
        Boolean b = roomService.updateById(officeRoomInfos);
        if (b) {
            return new ResultBean(200,"房间信息更新成功");
        }else {
            return new ResultBean(500,"房间信息更新失败");
        }
    }




    @RequestMapping("/delete")
    @ResponseBody
    public ResultBean delete(Integer roomId){
        Boolean b = roomService.removeById(roomId);
        if (b) {
            return new ResultBean(200,"删除成功");
        }else {
            return new ResultBean(500,"删除失败");
        }
    }


    @RequestMapping("/delete/batch")
    @ResponseBody
    public ResultBean deleteBatch(String roomIds){
        String[] strings = roomIds.split(",");
        for (String id : strings){
            Boolean b = roomService.removeById(id);
        }
        return new ResultBean(200,"删除成功");
    }
}

